package com.snake.springboot.custom.controller.fileoss;

import com.snake.springboot.custom.api.model.fileoss.OSSDelModel;
import com.snake.springboot.custom.api.model.fileoss.OSSPoliciesModel;
import com.snake.springboot.custom.api.model.fileoss.OSSUrlBathModel;
import com.snake.springboot.custom.api.model.fileoss.OSSUrlModel;
import com.snake.springboot.custom.api.response.fileoss.OSSInnerPoliciesRsp;
import com.snake.springboot.custom.api.response.fileoss.OSSPoliciesRsp;
import com.snake.springboot.custom.api.service.fileoss.FileActionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.snake.common.result.ResponseResult;
import org.snake.common.result.ResponseResultFactory;
import org.snake.common.utils.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>文件动作
 * 类{@code FileAction} </br>
 * 创建时间2025/5/7 14:58
 * @author yangxt
 * @see
 */
@Api(tags = "文件管理-文件动作")
@RestController
@RequestMapping("/file-action")
public class FileActionController {

    private FileActionService fileActionService;
    @Autowired
    public FileActionController(FileActionService fileActionService) {
        this.fileActionService = fileActionService;
    }

    @ApiOperation("获取OSS上传凭证-限制500M")
    @PostMapping("/generatePolicies")
    public ResponseResult<OSSPoliciesRsp> generatePolicies(
            @ApiParam(name = "对象", value = "传入json格式", required = true)
            @RequestBody OSSPoliciesModel model, HttpServletRequest request) {
        if (StringUtils.isEmpty(model.getModule())){
            String urlFrom = request.getHeader("urlFrom");
            if (StringUtils.isNotEmpty(urlFrom)){
                model.setModule(urlFrom);
            }else {
                model.setModule("common");
            }
        }
        OSSPoliciesRsp rsp = fileActionService.generatePolicies(model);
        return ResponseResultFactory.ucc("添加成功！！",rsp);
    }


    @ApiOperation("获取OSS上传凭证-不限制大小")
    @PostMapping("/generatePolicies-big")
    public ResponseResult<OSSPoliciesRsp> generatePoliciesBig(
            @ApiParam(name = "对象", value = "传入json格式", required = true)
            @RequestBody OSSPoliciesModel model, HttpServletRequest request) {
        if (StringUtils.isEmpty(model.getModule())){
            String urlFrom = request.getHeader("urlFrom");
            if (StringUtils.isNotEmpty(urlFrom)){
                model.setModule(urlFrom);
            }else {
                model.setModule("common");
            }
        }
        OSSPoliciesRsp rsp = fileActionService.generatePoliciesBig(model);
        return ResponseResultFactory.ucc("添加成功！！",rsp);
    }


    @ApiOperation("批量删除资源文件")
    @PostMapping("/delOssUrls")
    public ResponseResult<String> delOssUrls(@RequestBody OSSDelModel delModel){
        fileActionService.delOssUrls(delModel);
        return ResponseResultFactory.ucc("操作成功", "", 0, "OssFileController-2002}");
    }


    @ApiOperation("oss路径url获取密钥")
    @PostMapping("/generatePresignedUrl")
    public ResponseResult<String> generatePresignedUrl(@RequestBody OSSUrlModel urlModel){
        String presignedUrl = fileActionService.generatePresignedUrl(urlModel);
        return ResponseResultFactory.ucc("操作成功", presignedUrl, 0, "OssFileController-2002}");
    }

    @ApiOperation("批量获取路径")
    @PostMapping("/bath/generatePUrl")
    public ResponseResult<List<String>> generatePresignedUrl(@RequestBody OSSUrlBathModel urlModel){
        if (ListUtils.isEmpty(urlModel.getFileUrl())){
            return ResponseResultFactory.ucc("操作成功", new ArrayList<>(), 0, "OssFileController-2002}");
        }
        List<String> list = new ArrayList<>(urlModel.getFileUrl().size());
        for (String url:urlModel.getFileUrl()) {
            String presignedUrl = fileActionService.generatePresignedUrl(new OSSUrlModel(url, urlModel.getTimeOut()));
            list.add(presignedUrl);
        }
        return ResponseResultFactory.ucc("操作成功", list, 0, "OssFileController-2002}");
    }


    @ApiOperation("视频第一帧")
    @PostMapping("/video-first-frame")
    public ResponseResult<String> videoFirstFrame(@RequestBody OSSUrlModel urlModel){
        String presignedUrl = fileActionService.videoFirstFrame(urlModel);
        return ResponseResultFactory.ucc("操作成功", presignedUrl, 0, "OssFileController-2002}");
    }

    @ApiOperation("内部调用")
    @PostMapping("/inner-generatePolicies")
    public ResponseResult<OSSInnerPoliciesRsp> innerGeneratePolicies(
            @ApiParam(name = "对象", value = "传入json格式", required = true)
            @RequestBody OSSPoliciesModel model) {
        OSSInnerPoliciesRsp rsp = fileActionService.innerGeneratePolicies(model);
        return ResponseResultFactory.ucc("添加成功！！",rsp);
    }

}
